package com.zouxian.phantom;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;

public class TestPhantomJsDriver {
	public static PhantomJSDriver getPhantomJSDriver() {
		DesiredCapabilities dcaps = new DesiredCapabilities();
		// ssl证书支持
		dcaps.setCapability("acceptSslCerts", true);
		// 截屏支持
		dcaps.setCapability("takesScreenshot", true);
		// css搜索支持
		dcaps.setCapability("cssSelectorsEnabled", true);
		dcaps.setCapability("phantomjs.page.settings.XSSAuditingEnabled", true);
		dcaps.setCapability("phantomjs.page.settings.webSecurityEnabled", false);
		dcaps.setCapability("phantomjs.page.settings.localToRemoteUrlAccessEnabled", true);
		dcaps.setCapability("phantomjs.page.settings.XSSAuditingEnabled", true);

		dcaps.setCapability("phantomjs.page.settings.loadImages", false);
		// js支持
		dcaps.setJavascriptEnabled(true);

		// 驱动支持
		dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
				"D:\\developTools\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");

		 dcaps.setCapability("ignoreProtectedModeSettings", true);
//       org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
//       proxy.setProxyType(org.openqa.selenium.Proxy.ProxyType.MANUAL);
//       proxy.setHttpProxy("http://180.155.128.87:47593/");
//       dcaps.setCapability(CapabilityType.PROXY, proxy);
       //创建无界面浏览器对象
       PhantomJSDriver driver = new PhantomJSDriver(dcaps);
       driver.manage().timeouts().pageLoadTimeout(120,TimeUnit.SECONDS);
       driver.manage().timeouts().setScriptTimeout(120,TimeUnit.SECONDS);
       driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
       driver.manage().deleteAllCookies();
       driver.manage().window().setSize(new Dimension(1920,1080));
       return driver;

	}

	public static void main(String[] args) {
		WebDriver driver = getPhantomJSDriver();
		driver.get("http://www.baidu.com");
		System.out.println(driver.getCurrentUrl());

	}
}
